package com.example.audiorecord.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 自定义的波形图
 */
public class VadView extends View {
    private static final String TAG = "VadView";
    private Paint paint;
    private int audioSampleNum = 1280;
    private int widthPixels;
    private int heightPixels;
    short audio[];

    public void setAudio(short[] audio) {
        this.audio = audio;
    }

    public boolean haveGotWidthHeight = false;

    public void init() {

        widthPixels = this.getWidth();
        heightPixels = this.getHeight();
        Log.d(TAG,"VadView: w,h: " + widthPixels +" "+heightPixels);
        if (widthPixels > 0 && heightPixels > 0) {
            haveGotWidthHeight = true;
        }else{
            haveGotWidthHeight = false;
        }

        if (!haveGotWidthHeight) {
            getWidthLength();
        }


    }

    private void getWidthLength() {

        post(new Runnable() {
            @Override
            public void run() {
                VadView.this.widthPixels= VadView.this.getWidth();
                VadView.this.heightPixels = VadView.this.getHeight();

                haveGotWidthHeight = true;

                VadView.this.invalidate();
            }
        });


    }


    public VadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(1);
        init();
    }


    /**
     * 这里是波形图绘制的关键代码
     * @param canvas  画布
     * @param audio  音频数据流
     * 注：audioSampleNum的值越大，绘制出来的波形区域越小
     * 注：heightPixels/2  heightPixels就是屏幕高度，屏幕高度除以2才能让绘制出来的波形图处于屏幕正中间
     *
     * 绘制波形图原理就是遍历麦克风拾取的数据不停的绘制line，两组坐标可绘制一条直线
     *
     */
    protected void drawWave1(Canvas canvas, short audio[]){
        float startX, startY, stopX, stopY;
        for (int i = 0; i < audio.length - 1; i++){
            startX = (float)i/audioSampleNum * widthPixels;
            startY = heightPixels/2 + (float)audio[i]/32768 * heightPixels/2;
            stopX = (float)(i+1)/audioSampleNum * widthPixels;
            stopY = heightPixels/2 + (float)audio[i+1]/32768 * heightPixels/2;  
            canvas.drawLine(startX, startY, stopX, stopY, paint);
        }
    }


    protected void onDraw(Canvas canvas) {
        if (audio == null) {
            audio = new short[audioSampleNum];
        }
        drawWave1(canvas, audio);

    }


    public void refresh(){
        VadView.this.invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measure(widthMeasureSpec,true),measure(heightMeasureSpec,true));
    }

    private int measure(int measureSpec, boolean isWidth){
        int result;
        int size = MeasureSpec.getSize(measureSpec);
        int mode = MeasureSpec.getMode(measureSpec);
        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
        if (mode == MeasureSpec.EXACTLY){
            result = size;
        } else {
            result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight();
            result += padding;
            if (mode == MeasureSpec.AT_MOST){
                result = Math.max(result, size);
            }
        }
        return result;
    }
}
